<template>
  <el-form :model="formData" :rules="formRules" size="small" :inline="false" label-width="100px">
    <el-form-item label="主题" prop="title">
      <el-input v-model="formData.title" placeholder="请填写主题" maxlength="32" size="small"/>
    </el-form-item>
    <el-form-item label="员工" prop="staffId">
      <el-select v-model="formData.staffId" placeholder="请选择员工" size="small" filterable @change="staffChange">
        <el-option v-for="item in staffList" :key="item.id" :value="item.id" :label="item.name"/>
      </el-select>
    </el-form-item>
    <el-form-item label="计划离职日期" prop="occurDate">
      <el-date-picker v-model="formData.occurDate" type="date" placeholder="请选择计划离职日期" size="small"/>
    </el-form-item>
    <el-form-item label="实际离职日期" prop="endDate">
      <el-date-picker v-model="formData.endDate" type="date" placeholder="请选择实际离职日期" size="small"/>
    </el-form-item>
    <el-form-item label="薪资结算日期" prop="settlementDate">
      <el-date-picker v-model="formData.settlementDate" type="date" placeholder="请选择薪资结算日期" size="small"/>
    </el-form-item>
    <el-form-item label="离职类型" prop="staffLeaveType">
      <el-select v-model="formData.staffLeaveType" placeholder="请选择离职类型" size="small">
        <el-option v-for="item in dictMap['StaffLeaveType']" :key="item.value" :value="item.value" :label="item.name"/>
      </el-select>
    </el-form-item>
    <el-form-item label="离职原因" prop="content">
      <editor v-model="formData.content" />
    </el-form-item>
    <el-form-item label="优先度" prop="priority">
      <el-radio-group v-model="formData.priority" size="small">
        <el-radio :label="0" size="small">普通</el-radio>
        <el-radio :label="1" size="small">高</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="备注信息" prop="remark">
      <el-input v-model="formData.remark" placeholder="请填写备注信息" :rows="3" type="textarea" size="small"/>
    </el-form-item>
  </el-form>
</template>

<script>
// 正式离职
import {onMounted, reactive, toRefs} from 'vue'
import {listByState} from '/@/api/oa/staff'
import {getDictByCodes} from '/@/api/sys/dict'
import {getLastPlanLeave} from '/@/api/oa/staff/dimission'
import Editor from '/@/components/editor/index.vue'

export default {
  name: 'OfficialDimission',
  components: {
    Editor
  },
  setup() {
    const state = reactive({
      formData: {
        title: '正式离职',
        staffId: null,
        content: null,    // 离职原因
        occurDate: null,     // 计划离职日期
        endDate: null,  // 实际离职日期
        settlementDate: null,   // 薪资结算时间
        staffLeaveType: null,   // 离职类型
        remark: null,   // 备注信息
        state: 2,           // 状态 -1草稿 0未开始 1进行中 2已完成 3已退回  4已取消 5已撤回
        priority: 0        // 优先级 0普通 1重要
      },
      formRules: {
        title: {required: true, message: '请填写主题', trigger: 'blur'},
        staffId: {required: true, message: '请选择员工', trigger: 'change'}
      },
      dictMap: {},
      staffList: []
    })

    const getStaffData = async () => {
      try {
        const res = await listByState('')
        state.staffList = res.data
      } catch (e) {
        console.log(e)
      }
    }
    const staffChange = async (val) => {
      try {
        const res = await getLastPlanLeave(val)
        if (res.data) {
          state.formData.occurDate = res.data.planDate
          state.formData.settlementDate = res.data.salaryPayDate
          state.formData.staffLeaveType = res.data.type
          state.formData.content = res.data.reason
        }
      } catch (e) {
        console.log(e)
      }
    }
    onMounted(() => {
      getDictByCodes(['StaffLeaveType']).then(res => state.dictMap = res.data).catch(e => console.log(e))
      getStaffData()
    })

    return {
      ...toRefs(state),
      getStaffData,
      staffChange
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
